package com.nbjtjc.safe.elasticjob;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.nbjtjc.common.bean.PLANFREQUENCY;
import com.nbjtjc.common.bean.TASKSTATUS;
import com.nbjtjc.safe.filter.SessionFilter;
import com.nbjtjc.safe.mapper.safe.SafeDangerInspectPlanMapper;
import com.nbjtjc.safe.mapper.safe.SafeDangerInspectTaskMapper;
import com.nbjtjc.safe.model.safe.SafeDangerInspectPlan;
import com.nbjtjc.safe.model.safe.SafeDangerInspectTask;
import com.nbjtjc.safe.service.safe.SafeDangerInspectTaskServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

public class GeneraterTaskJob implements SimpleJob {
    private static final Logger log = LoggerFactory.getLogger(GeneraterTaskJob.class);

    @Autowired
    private SafeDangerInspectPlanMapper planMapper;

    @Autowired
    private SafeDangerInspectTaskMapper taskMapper;

    @Autowired
    private SafeDangerInspectTaskServiceImpl taskService;

    @Override
    public void execute(ShardingContext shardingContext) {
        System.out.println("开始批量任务");
        log.info("——————计划状态更新批量任务 开始——————");
        SafeDangerInspectPlan safePlan = new SafeDangerInspectPlan();
//        //查找未开始的计划，若在时间范围内，则更新状态
//        safePlan.setStatus(2);//查找未开始的计划
//        List<SafeDangerInspectPlan> planList = planMapper.search(safePlan);
//        planList.forEach(safeDangerInspectPlan -> {
//            if (safeDangerInspectPlan.getStartTime().getTime()<(new Date()).getTime()){
//                planMapper.updatePlanStatus(safeDangerInspectPlan.getId(),0);
//                taskService.generateTaskByPlan(safeDangerInspectPlan.getId(),0);
//            }
//        });
        //查找进行中的计划，根据检查频率生成检查任务‹
        safePlan.setStatus(0);
        List<SafeDangerInspectPlan>  planList=planMapper.search(safePlan);
        log.info("查询进行中的任务，计划数量："+planList.size());
        planList.forEach(safeDangerInspectPlan -> {
            if(safeDangerInspectPlan.getEndTime().getTime()<(new Date()).getTime()){
                //已到达结束时间
                log.info("计划："+safeDangerInspectPlan.getName()+"已到达结束时间，结束时间为："+ DateUtil.format(safeDangerInspectPlan.getEndTime(),"yyyy-MM-dd hh:mm"));
                planMapper.updatePlanStatus(safeDangerInspectPlan.getId(),1);
            }else{
                log.info("计划："+safeDangerInspectPlan.getName()+"未到结束时间，结束时间为："+ DateUtil.format(safeDangerInspectPlan.getEndTime(),"yyyy-MM-dd hh:mm"));
                publicTaskPlan(safeDangerInspectPlan);
            }
        });

        log.info("——————计划状态更新批量任务 结束——————");
    }


    private void publicTaskPlan(SafeDangerInspectPlan safePlan){
        SafeDangerInspectTask task=new SafeDangerInspectTask();
        task.setInspectPlanId(safePlan.getId());
        task.setTaskStatus(TASKSTATUS.PLAN.getCode());
        List<SafeDangerInspectTask> taskList=taskMapper.select(task);
        log.info("计划："+safePlan.getName()+"计划中任务数量未："+ taskList.size());
        if(CollectionUtil.isNotEmpty(taskList)){
            log.info("计划："+safePlan.getName()+"计划中任务数量未："+ taskList.size());
            taskService.publicTaskInDays(taskList,2,safePlan.getId());
        }
    }


    private void genteraterTaskByFrequency(SafeDangerInspectPlan plan) {
        long freqTimeInterval = PLANFREQUENCY.getTimeInterval(plan.getFrequency());
        SafeDangerInspectTask latestTask = taskMapper.latestTask(plan.getId());
        if (ObjectUtil.isNotNull(latestTask) &&
                !plan.getFrequency().equals(PLANFREQUENCY.NOREPEAT.getCode())) {
            latestTask.getCreateTime();
            long timeInterval = ((new Date()).getTime() - latestTask.getCreateTime().getTime()) / 1000;
            if (timeInterval >= freqTimeInterval) {
                //生成task
                taskService.generateTaskByPlan(plan.getId(),0);
            }
        }

    }
}
